package cn.tedu.boot11.controller;

import cn.tedu.boot11.entity.Emp;
import cn.tedu.boot11.utils.DBUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@RestController
public class EmpController {
    @RequestMapping("/insert")
    public String insert(Emp emp){
        System.out.println("emp = " + emp);
        try (Connection connection= DBUtils.getConn()
        ){

            String sql="insert into myemp values(null,?,?,?)";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1, emp.getName());
            ps.setInt(2,emp.getSal());
            ps.setString(3, emp.getJob());
            ps.executeUpdate();

        }catch (SQLException sqlException){
            sqlException.printStackTrace();
        }
        return "添加成功";
    }
    @RequestMapping("/select")
    public String select(){
        ArrayList<Emp> list=new ArrayList<>();
        try(Connection connection= DBUtils.getConn()
        ){
            String sql="select * from myemp";
            PreparedStatement ps=connection.prepareStatement(sql);
            ResultSet rs=ps.executeQuery();
            while (rs.next()){
                int id= rs.getInt(1);
                String name=rs.getString(2);
                int sal=rs.getInt(3);
                String job=rs.getString(4);
                //把查询到的四个信息封装到Emp对象里面
                Emp emp= new Emp(id,name,sal,job);
                list.add(emp);
            }
        }catch (SQLException sqlException){
            sqlException.printStackTrace();
        }
        String html="<table border=1>";
        html+="<caption></caption>";
        html+="<tr><th>id</th><th>名字</th><th>工资</th><th>工作</th></tr>";
        //遍历集合,并把数据添加到表格中
        for(Emp emp : list ){
            html+="<tr>";
            html+="<td>"+emp.getId()+"</td>";
            html+="<td>"+emp.getName()+"</td>";
            html+="<td>"+emp.getSal()+"</td>";
            html+="<td>"+emp.getJob()+"</td>";
            html+="<td><a href='/delete?id="+emp.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }

    @RequestMapping("/delete")
    public String delete(int id){

        try(Connection connection = DBUtils.getConn()
        ) {
            String sql="delete from myemp where id =?";
            PreparedStatement ps= connection.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();

        }catch (SQLException sqlException){
            sqlException.printStackTrace();
        }

        return "删除成功<a href='/select'>返回列表页面</a>";
    }

    @RequestMapping("/update")
    public String update(Emp emp){
        try (Connection connection=DBUtils.getConn()
        ){
            String sql="update myemp set name=?,sal=?,job=? where id=?";
            PreparedStatement ps= connection.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setInt(2,emp.getSal());
            ps.setString(3,emp.getJob());
            ps.setInt(4,emp.getId());
            ps.executeUpdate();
        }catch (SQLException sqlException){
            sqlException.printStackTrace();
        }
        return "修改完成<a href='/select'>返回参数列表</a>";
    }
}
